<?php
/**
* @file $Id: Enrollment.inc.php 437 2007-04-23 00:57:51Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

include_once('modules/Students/includes/functions.php');

if(($_REQUEST['month_values'] && $_POST['month_values']) || ($_REQUEST['values']['STUDENT_ENROLLMENT'] && $_POST['values']['STUDENT_ENROLLMENT']))
{
	if(!$_REQUEST['values']['STUDENT_ENROLLMENT']['new']['ENROLLMENT_CODE'] && !$_REQUEST['month_values']['STUDENT_ENROLLMENT']['new']['START_DATE'])
	{
		unset($_REQUEST['values']['STUDENT_ENROLLMENT']['new']);
		unset($_REQUEST['day_values']['STUDENT_ENROLLMENT']['new']);
		unset($_REQUEST['month_values']['STUDENT_ENROLLMENT']['new']);
		unset($_REQUEST['year_values']['STUDENT_ENROLLMENT']['new']);
	}
	else
	{
		$date = $_REQUEST['day_values']['STUDENT_ENROLLMENT']['new']['START_DATE'].'-'.$_REQUEST['month_values']['STUDENT_ENROLLMENT']['new']['START_DATE'].'-'.$_REQUEST['year_values']['STUDENT_ENROLLMENT']['new']['START_DATE'];
		$found_RET = DBGet(DBQuery("SELECT ID FROM STUDENT_ENROLLMENT WHERE STUDENT_ID='".UserStudentID()."' AND SYEAR='".UserSyear()."' AND '$date' BETWEEN START_DATE AND END_DATE"));
		if(count($found_RET))
		{
			unset($_REQUEST['values']['STUDENT_ENROLLMENT']['new']);
			unset($_REQUEST['day_values']['STUDENT_ENROLLMENT']['new']);
			unset($_REQUEST['month_values']['STUDENT_ENROLLMENT']['new']);
			unset($_REQUEST['year_values']['STUDENT_ENROLLMENT']['new']);
			echo ErrorMessage(array(_('The student is already enrolled on that date, and could not be enrolled a second time on the date you specified.  Please fix, and try enrolling the student again.')));
		}
	}
	
	$iu_extra['STUDENT_ENROLLMENT'] = "STUDENT_ID='".UserStudentID()."' AND ID='__ID__'";
	$iu_extra['fields']['STUDENT_ENROLLMENT'] = 'ID,SYEAR,STUDENT_ID,';
	$iu_extra['values']['STUDENT_ENROLLMENT'] = "nextval('STUDENT_ENROLLMENT_SEQ'),'".UserSyear()."','".UserStudentID()."',";
	if(!$new_student)
		SaveData($iu_extra,'',$field_names);
}

$functions = array('START_DATE'=>'_makeStartInput','END_DATE'=>'_makeEndInput','SCHOOL_ID'=>'GetSchool');
unset($THIS_RET);
$RET = DBGet(DBQuery("SELECT e.ID,e.ENROLLMENT_CODE,e.START_DATE,e.DROP_CODE,e.END_DATE,e.END_DATE AS END,e.SCHOOL_ID,e.NEXT_SCHOOL,e.CALENDAR_ID,e.INCLUDE_IN_CLASS_RANK FROM STUDENT_ENROLLMENT e WHERE e.STUDENT_ID='".UserStudentID()."' AND e.SYEAR='".UserSyear()."' ORDER BY e.START_DATE"),$functions);

$link['add'] = false;
$add = true;
if(count($RET))
{
	foreach($RET as $value)
	{
		if(($value['DROP_CODE']=='' || !$value['DROP_CODE']) && ($value['END']=='' || !$value['END']))
			$add = false;
	}
}
if($add)
	$link['add']['html'] = array('START_DATE'=>_makeStartInput('','START_DATE'),'SCHOOL_ID'=>_makeSchoolInput('','SCHOOL_ID'));

$columns = array('START_DATE'=>_('Attendance Start Date this School Year'),'END_DATE'=>_('Dropped'),'SCHOOL_ID'=>_('School'));
$_REQUEST['modfunc'] = '';
$schools_RET = DBGet(DBQuery("SELECT ID,TITLE FROM SCHOOLS WHERE ID!='".UserSchool()."'"));
$next_school_options = array(UserSchool()=>_('Next grade at current school'),'0'=>_('Retain'),'-1'=>_('Do not enroll after this school year'));
if(count($schools_RET))
{
	foreach($schools_RET as $school)
		$next_school_options[$school['ID']] = $school['TITLE'];
}

$calendars_RET = DBGet(DBQuery("SELECT CALENDAR_ID,DEFAULT_CALENDAR,TITLE FROM ATTENDANCE_CALENDARS WHERE SYEAR='".UserSyear()."' AND SCHOOL_ID='".UserSchool()."' ORDER BY DEFAULT_CALENDAR ASC"));
if(count($calendars_RET))
{
	foreach($calendars_RET as $calendar)
		$calendar_options[$calendar['CALENDAR_ID']] = $calendar['TITLE'];
}
if($_REQUEST['student_id']!='new')
{
	if(count($RET))
		$id = $RET[count($RET)]['ID'];
	else
		$id = 'new';

	ListOutput($RET,$columns,_('Enrollment Record'),_('Enrollment Records'),$link);
	$next_school_name = 'values[STUDENT_ENROLLMENT]['.$id.'][NEXT_SCHOOL]';
	if($id)
		$next_school = $RET[count($RET)]['NEXT_SCHOOL'];
	$calendar_name = 'values[STUDENT_ENROLLMENT]['.$id.'][CALENDAR_ID]';
	if($id)
		$calendar = $RET[count($RET)]['CALENDAR_ID'];
	$include_in_class_rank = $RET[count($RET)]['INCLUDE_IN_CLASS_RANK'];
	$include_in_class_rank_name = 'values[STUDENT_ENROLLMENT]['.$id.'][INCLUDE_IN_CLASS_RANK]';
	$div = true;
}
else
{
	ListOutput($RET,$columns,_('Enrollment Record'),_('Enrollment Records'),$link,array(),array('count'=>false));
	$next_school_name = 'values[STUDENT_ENROLLMENT][new][NEXT_SCHOOL]';
	$next_school = UserSchool();
	$calendar_name = 'values[STUDENT_ENROLLMENT][new][CALENDAR_ID]';
	$include_in_class_rank = 'Y';
	$include_in_class_rank_name = 'values[STUDENT_ENROLLMENT][new][INCLUDE_IN_CLASS_RANK]';
	$div = false;
}
echo '<CENTER><TABLE><TR>'.(count($calendars_RET)>1?'<TD>'.SelectInput($calendar,$calendar_name,_('Calendar').' <font color=red>*</font>',$calendar_options,' - '._('Choose').' - ','',$div).'</TD><TD width=30></TD>':'<TD><INPUT type=hidden name='.$calendar_name.' value='.$calendars_RET[1]['CALENDAR_ID'].'></TD>').
	'<TD>'.SelectInput($next_school,$next_school_name,_('Rolling / Retention Options'),$next_school_options,false,'',$div).'</TD>'.
	'<TD width=30></TD><TD>'.CheckboxInput($include_in_class_rank,$include_in_class_rank_name,_('Include in Class Rank'),'',false,_('Yes'),_('No'),$div).'</TD>'.
	'</TR></TABLE></CENTER>';

?>